(function () {
	nsbase.views.otp = new  OTPFunc ();

	function OTPFunc () {
		var self = this;
		self.init = init;



		var otpActions = nsbase.config.actionUrl.otp,
			utilities = nsbase.utility,
			validations = nsbase.utility.validations;

		var cssClass = {
			error:"error",
			loading:"loading",
			success:"success",
			result:"result",
			selected:"selected"
		};

		var $txtDeviceId = $("#txtDeviceId"),
			$deviceStatus = $("#deviceStatus"),
			$deviceDetail = $("#deviceDetail"),
			$btnSetDevice = $(".btnSetDevice"),
			$btnReset = $(".btnReset"),
			$btnViewDevice = $(".btnViewDevice"),
			$etForm = $(".et_form"),
			$result = $etForm.find("." + cssClass.result);

		

		function init () {
			bindHandler();
			bindTabs();
		}

		function bindTabs () {
			var $descContainer = $(".desc"),
				$tabs = $descContainer.find(".nav-desc li"),
				$descs = $descContainer.children('p');

			$tabs.click(function () {
				var $sender = $(this);
				if($sender.hasClass(cssClass.selected)){
					return false;
				}
				$descs.hide();
				$tabs.removeClass(cssClass.selected);
				var className = $sender.attr("class");
				$sender.addClass(cssClass.selected);
				$descs.filter('.'+className).show();
			});
		}

		function bindHandler () {
			// validate emial format and emial is used by others
			$txtDeviceId.blur(function() {
				var sender = $(this),
					msgSpan = sender.next("span");
				var param = {
					deviceId: sender.val()
				};

				if(!param.deviceId){
					return;
				}
				msgSpan.empty().removeClass(cssClass.error).addClass(cssClass.loading);
				// sync validate user email is used or not
				nsbase.ajax.request(otpActions.validateDeviceId, param, function(result) {
					msgSpan.removeClass(cssClass.loading);
					if(result.Success && result.Data){
						msgSpan.addClass(cssClass.success);
					} else {
						msgSpan.addClass(cssClass.error).text(result.Message);
					}
				},function(){
					msgSpan.removeClass(cssClass.loading);
				});
			});


			$btnSetDevice.click(function() {
				var sender = $(this),
					loadingDiv = $etForm.find("." + cssClass.loading);
					
				inputEmptyValidate([$txtDeviceId]);
				if ($("." + cssClass.error).length > 0) {
					return false;
				}

				$etForm.mask();
				var param = {
					deviceId: $txtDeviceId.val()
				}
				nsbase.ajax.request(otpActions.setDeviceId, param, function(result) {
					$etForm.unmask();
					if(result.Success && result.Data){
						$result.text(utilities.stringFormat('序列号 {0} 正在等待烧录,请长按小P按钮!',param.deviceId));
					} else {
						$result.text(result.Message);
					}
					$result.show();
				},function(e){
					$etForm.unmask();
					utilities.errorDialog(e.message);
				});
			});
		
			$btnReset.click(function() {
				$etForm.mask();
				nsbase.ajax.request(otpActions.resetDevice, null, function(result) {
					$etForm.unmask();
					if(result.Success && result.Data){
						$result.text("服务器环境已设置为小P重置模式,请长按小P按钮.");
						$result.show();
					} else {
						$result.text(result.Message);
						$result.show();
					}
				},function(e){
					$etForm.unmask();
					utilities.errorDialog(e.message);
				});
			});

			$btnViewDevice.click(function() {
				$result.hide();
				$result.empty();
				$etForm.mask();
				nsbase.ajax.request(otpActions.getDeviceData, null, function(result) {
					$etForm.unmask();
					if(result.Success){
						$deviceDetail.show();
						var data = result.Data;
						var devStatusEnum = nsbase.enums.deviceStatus.getEnumByValue(data.DeviceStatus);
						$deviceStatus.text(utilities.stringFormat("当前小P序列号：{0}  状态：{1} ",data.DeviceId,devStatusEnum.text));
						createRunMinuteGrid(data.RunMinuteData);
						createRunDailyGrid(data.RunDailyData);
						createSleepMinuteGrid(data.SleepMinuteData);
						createSleepDailyGrid(data.SleepDailyData);
					} else {
						$deviceDetail.hide();
						$result.text(result.Message);
						$result.show();
					}
				},function(e){
					$etForm.unmask();
					utilities.errorDialog(e.message);
				});
			});

		}


		function inputEmptyValidate(inputs) {
			$.each(inputs, function(i, item) {
				var labelDesc = utilities.trimEnd(item.prev().text(),'：');
				var msgSpan = item.next("span");
				if(msgSpan.hasClass(cssClass.error)){
					return;
				}
				msgSpan.removeClass(cssClass.error);
				if (validations.isNullOrWhiteSpace(item.val())) {
					msgSpan.addClass(cssClass.error).text(utilities.stringFormat("{0}不能为空!", labelDesc));
				} 
			});
		}

		

		function createRunMinuteGrid (gridData) {
			var gridConfig = {
				renderTo:"runMinuteGrid",
				height:500,
				rowNumber:true,
				columns:[
					{title:"卡路里",dataIndex:"calorie",width:145},
					{title:"路程(米)",dataIndex:"miles",width:150},
					{title:"步数",dataIndex:"steps",width:150},
					{title:"小时",dataIndex:"hour",width:100},
					{title:"分钟",dataIndex:"minute",width:100},
					{title:"记录时间",dataIndex:"daykey",width:100},
					{title:"上传时间",dataIndex:"time",width:150}
				],
				rowSelectable:"none"
			}
			var tempGrid = new nsbase.controls.grid(gridConfig);
			tempGrid.showData(gridData);
		}

		function createRunDailyGrid (gridData) {
			var gridConfig = {
				renderTo:"runDailyGrid",
				height:500,
				rowNumber:true,
				columns:[
					{title:"卡路里",dataIndex:"totalcalorie",width:145},
					{title:"路程(米)",dataIndex:"totalmileages",width:150},
					{title:"步数",dataIndex:"totalsteps",width:150},
					{title:"分钟",dataIndex:"totaltime",width:100},
					{title:"记录时间",dataIndex:"daykey",width:100},
					{title:"上传时间",dataIndex:"time",width:150}
				],
				rowSelectable:"none"
			}
			var tempGrid = new nsbase.controls.grid(gridConfig);
			tempGrid.showData(gridData);
		}

		function createSleepMinuteGrid (gridData) {
			var gridConfig = {
				renderTo:"sleepMinuteGrid",
				height:500,
				rowNumber:true,
				columns:[
					{title:"动弹次数",dataIndex:"action_cnt",width:150},
					{title:"小时",dataIndex:"hour",width:100},
					{title:"分钟",dataIndex:"minute",width:100},
					{title:"记录时间",dataIndex:"daykey",width:100},
					{title:"上传时间",dataIndex:"time",width:150}
				],
				rowSelectable:"none"
			}
			var tempGrid = new nsbase.controls.grid(gridConfig);
			tempGrid.showData(gridData);
		}

		function createSleepDailyGrid (gridData) {
			var gridConfig = {
				renderTo:"sleepDailyGrid",
				height:500,
				rowNumber:true,
				columns:[
					{title:"睡觉开始",dataIndex:"start_sleep",width:100},
					{title:"睡觉结束",dataIndex:"end_sleep",width:100},
					{title:"记录时间",dataIndex:"daykey",width:100},
					{title:"上传时间",dataIndex:"time",width:150}
				],
				rowSelectable:"none"
			}
			var tempGrid = new nsbase.controls.grid(gridConfig);
			tempGrid.showData(gridData);
		}
	}
})();